Demographic Characteristics

Row

Vital Sign

Row

Description

This is the world map showing the life expectancy (years) of each country from 2000 to 2016.

 

Take the year of 2015 as an example,

 
  • North America, Western and Northern Europe, Japan, New Zealand and Australia show longer life expectancy compared with other regions.
 
  • Central and Southern Africa have relatively shorter life expectancy because the color is lighter,

Boxplot

Comorbidities

Row

Comorbidities

Row

Description

This is the world map showing the life expectancy (years) of each country from 2000 to 2016.

 

Take the year of 2015 as an example,

 
  • North America, Western and Northern Europe, Japan, New Zealand and Australia show longer life expectancy compared with other regions.
 
  • Central and Southern Africa have relatively shorter life expectancy because the color is lighter,

Lab Results Analysis

Row

Lab Results Analysis

Row

Description

This is the world map showing the life expectancy (years) of each country from 2000 to 2016.

 

Take the year of 2015 as an example,

 
  • North America, Western and Northern Europe, Japan, New Zealand and Australia show longer life expectancy compared with other regions.
 
  • Central and Southern Africa have relatively shorter life expectancy because the color is lighter,

Boxplot

---
title: "Mortality Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    source_code: embed
runtime: shiny
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(plotly)
library(shiny)
library(data.table)
library(reshape2)

# read the data
mortality_data <- 
  read_csv("mortality_data_cleaned.csv")

# Change the factor variables for EDA 

col_names <- c("group", "outcome", "hypertensive", 
                         "atrialfibrillation", "chd_with_no_mi", "diabetes", 
                         "deficiencyanemias", "depression", "hyperlipemia", 
                         "renal_failure", "copd")

mortality_data[,col_names] <- lapply(mortality_data[,col_names] , factor)


# Manually recoding factors to their meaningful character values
mortality_data_EDA <- mortality_data %>%
  mutate(
    group = recode(group, `1` = "Group 1", `2` = "Group 2"),
    gender = recode(gender, `1` = "Male", `2` = "Female"),
    outcome = recode(outcome, `0` = "Alive", `1` = "Death")
  ) %>%
  mutate_if(is.factor, as.character) # Converts all remaining factors to characters

# Function to recode factor variables to 'Yes' or 'No'
convert_factors_to_yes_no <- function(df, comorbidity_columns) {
  df <- df %>%
    mutate(across(all_of(comorbidity_columns), ~ ifelse(as.character(.) == "1", "Yes", "No")))
  return(df)
}

# List of comorbidity columns to convert
comorbidity_columns <- c("hypertensive", "atrialfibrillation", "chd_with_no_mi", 
                         "diabetes", "deficiencyanemias", "depression", 
                         "hyperlipemia", "renal_failure", "copd")

# Apply the function to the mortality_data_EDA dataframe
mortality_data_EDA <- convert_factors_to_yes_no(mortality_data_EDA, comorbidity_columns)


# function to correct the title format 
formatVarName <- function(variable) {
  variable <- gsub("_", " ", variable)  # Replace underscores with spaces
  variable <- tools::toTitleCase(variable)  # Capitalize the first letter of each word
  return(variable)
}

```


Demographic Characteristics
===================================== 

Column {.sidebar}
-----------------------------------------------------------------------


```{r}
selectInput(
    inputId = "vital_sign_index_x",
    label = h3("Select Index"),
    choices = c("Heart Rate" = "heart_rate", 
                "Systolic Blood Pressure" = "systolic_blood_pressure", 
                "Diastolic Blood Pressure" = "diastolic_blood_pressure", 
                "Respiratory Rate" = "respiratory_rate", 
                "Temperature" = "temperature", 
                "SpO2" = "sp_o2", 
                "Urine Output" = "urine_output"),
    selected = "heart_rate"
  )

```




Row{data-height=650}
-----------------------------------------------------------------------

### Vital Sign

```{r}

renderPlotly({
    variable <- input$vital_sign_index_x
    formattedVarName <- formatVarName(variable)
  
    plot_ly(data = mortality_data_EDA %>% drop_na(.data[[variable]]),
            x = ~.data[[variable]],
            color = ~as.factor(outcome),
            type = 'histogram',
            opacity = 0.6) %>%
      layout(barmode = "overlay",
             title = paste(formattedVarName, "Levels by Outcome"),
             xaxis = list(title = formattedVarName),
             yaxis = list(title = "Density"))
  })


```

Row {.tabset .tabset-fade}
-------------------------------------


### Description


This is the world map showing the life expectancy (years) of each country from 2000 to 2016. 

\ \par
Take the year of 2015 as an example,

\ \par
  * North America, Western and Northern Europe, Japan, New Zealand and Australia show longer life expectancy compared with other regions.
  
\ \par
  * Central and Southern Africa have relatively shorter life expectancy because the color is lighter,
  
### Boxplot

```{r}
renderPlotly({
  variable <- input$vital_sign_index_x
  formattedVarName <- formatVarName(variable)
  
  plot_ly(mortality_data_EDA %>% drop_na(.data[[variable]]), 
          x = ~as.factor(outcome),
          y = ~.data[[variable]],
          color = ~as.factor(outcome),
          type = "box") %>%
    layout(title = paste(formattedVarName, "Levels by Outcome"),
           xaxis = list(title = "Outcome"),
           yaxis = list(title = formattedVarName))
})

```



Comorbidities
===================================== 

Column {.sidebar}
-----------------------------------------------------------------------

```{r}
selectInput(
    inputId = "como_index_y",
    label = h3("Select Comorbidity"),
    choices = c("Hypertensive" = "hypertensive", 
                "Atrialfibrillation" = "atrialfibrillation", 
                "CHD with no MI"= "chd_with_no_mi",
                "Diabetes" = "diabetes", 
                "Deficiencyanemias"= "deficiencyanemias", 
                "Depression"= "depression", 
                "Hyperlipemia"= "hyperlipemia", 
                "Renal Failure"= "renal_failure", 
                "COPD"= "copd"),
    selected = "depression"
  )


```


Row{data-height=650}
-----------------------------------------------------------------------

### Comorbidities

```{r}
comorbidities <- c("hypertensive", "atrialfibrillation", "chd_with_no_mi", 
                         "diabetes", "deficiencyanemias", "depression", 
                         "hyperlipemia", "renal_failure", "copd")

renderPlotly({
    variable <- input$como_index_y
    formattedVarName <- formatVarName(variable)
    # Melt the data into a long format for plotting
    mortality_long <- melt(mortality_data_EDA, id.vars = "outcome", measure.vars = comorbidities)
    
    # Filter for the selected comorbidity
    filtered_data <- mortality_long %>% 
      filter(variable == input$como_index_y) %>%
      mutate(Count = ifelse(value == "Yes", 1, 0)) %>%
      group_by(outcome, variable) %>%
      summarise(Yes = sum(Count), No = n() - sum(Count)) %>%
      gather(key = "Presence", value = "Count", -outcome, -variable)
    
    # Create the stacked bar chart
    plot_ly(data = filtered_data, 
            x = ~outcome, 
            y = ~Count, 
            color = ~Presence, 
            type = 'bar', 
            text = ~paste(Count, "cases"), 
            hoverinfo = 'text+x+y') %>%
      layout(title = paste("Distribution of", formattedVarName, "by Outcome"),
             yaxis = list(title = "Percentage", tickformat = ",.0%"),
             xaxis = list(title = formattedVarName),
             barmode = 'stack')
  })
```


```{r eval=FALSE}
renderPlotly({
  variable <- input$como_index_y
  formattedVarName <- formatVarName(variable)

    # Create the stacked bar chart
    plot_ly(data = mortality_long, 
            x = ~variable, 
            y = ~value, 
            color = ~outcome, 
            type = 'bar', 
            text = ~paste(value, "cases")) %>%
      layout(yaxis = list(title = "Count"), 
             barmode = 'stack', 
             xaxis = list(title = ""))
  })

```

Row {.tabset .tabset-fade}
-------------------------------------


### Description


This is the world map showing the life expectancy (years) of each country from 2000 to 2016. 

\ \par
Take the year of 2015 as an example,

\ \par
  * North America, Western and Northern Europe, Japan, New Zealand and Australia show longer life expectancy compared with other regions.
  
\ \par
  * Central and Southern Africa have relatively shorter life expectancy because the color is lighter,
  


Lab Results Analysis
===================================== 

Column {.sidebar}
-----------------------------------------------------------------------


```{r}
selectInput(
    inputId = "index",
    label = h3("Select Index"),
    choices = c("Creatinine" = "creatinine", 
                "Lactic Acid" = "lactic_acid", 
                "Urea Nitrogen" = "urea_nitrogen", 
                "Leucocyte" = "leucocyte", 
                "Glucose" = "glucose", 
                "Anion Gap" = "anion_gap", 
                "PCO2" = "pco2"),
    selected = "creatinine"
  )

```




Row{data-height=650}
-----------------------------------------------------------------------

### Lab Results Analysis

```{r}

renderPlotly({
    variable <- input$index
    formattedVarName <- formatVarName(variable)
  
    plot_ly(data = mortality_data_EDA %>% drop_na(.data[[variable]]),
            x = ~.data[[variable]],
            color = ~as.factor(outcome),
            type = 'histogram',
            opacity = 0.6) %>%
      layout(barmode = "overlay",
             title = paste(formattedVarName, "Levels by Outcome"),
             xaxis = list(title = formattedVarName),
             yaxis = list(title = "Density"))
  })



```

Row {.tabset .tabset-fade}
-------------------------------------


### Description


This is the world map showing the life expectancy (years) of each country from 2000 to 2016. 

\ \par
Take the year of 2015 as an example,

\ \par
  * North America, Western and Northern Europe, Japan, New Zealand and Australia show longer life expectancy compared with other regions.
  
\ \par
  * Central and Southern Africa have relatively shorter life expectancy because the color is lighter,
  
### Boxplot

```{r}
renderPlotly({
  variable <- input$index
  formattedVarName <- formatVarName(variable)
  
  plot_ly(mortality_data_EDA %>% drop_na(.data[[variable]]), 
          x = ~as.factor(outcome),
          y = ~.data[[variable]],
          color = ~as.factor(outcome),
          type = "box") %>%
    layout(title = paste(formattedVarName, "Levels by Outcome"),
           xaxis = list(title = "Outcome"),
           yaxis = list(title = formattedVarName))
})

```